package cn.cvs.servlet;

import cn.cvs.dao.SysUserMapper;
import cn.cvs.entity.EdocEntry;
import cn.cvs.service.EdocEntryService;
import cn.cvs.service.impl.EdocEntryServiceImpl;
import cn.cvs.utils.MyBatisUtil;
import org.apache.ibatis.session.SqlSession;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @auth 吴阳基
 * @date 2024/01/05 16:19
 */
@WebServlet(urlPatterns = "/Index")
public class AddtoServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out=response.getWriter();
        EdocEntryService cardService = new EdocEntryServiceImpl();
        SqlSession sqlSession=null;
        int add=0;
        try {
            sqlSession= MyBatisUtil.createSqlSession();
            String  title=request.getParameter("title");
            String  summray=request.getParameter("summray");
            String   uploaduser=request.getParameter("uploaduser");
            String  createdate=request.getParameter("createdate");
            EdocEntry card=new EdocEntry();
            card.setTitle(title);
            card.setSummray(summray);
            card.setUploaduser(uploaduser);
            card.setCreatedate(createdate);
             add=sqlSession.getMapper(SysUserMapper.class).add(card);
            sqlSession.commit();
        } catch (Exception e) {
            e.printStackTrace();
            sqlSession.rollback();
            add=0;
        }finally {
            MyBatisUtil.closeSqlSession(sqlSession);
        }
        if (add>0){
            out.print("<script>alert('新增成功');</script>");
            out.print("<script>location.href='list'</script>");
        } else {
            out.print("<script>alert('新增失败');</script>");
            out.print("<script>location.href='list'</script>");
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}
